home *** CD-ROM | disk | FTP | other *** search
- VERSION 5.00
- Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
- Begin VB.Form frmMain
- BorderStyle = 1 'Fixed Single
- Caption = "Service Manager Example #1"
- ClientHeight = 3015
- ClientLeft = 45
- ClientTop = 615
- ClientWidth = 7425
- Icon = "frmMain.frx":0000
- LinkTopic = "Form1"
- MaxButton = 0 'False
- ScaleHeight = 3015
- ScaleWidth = 7425
- StartUpPosition = 1 'CenterOwner
- Begin VB.Timer Timer
- Enabled = 0 'False
- Interval = 2000
- Left = 6600
- Top = 2520
- End
- Begin VB.TextBox txtParameters
- Height = 285
- Left = 120
- TabIndex = 2
- Top = 2640
- Width = 6015
- End
- Begin VB.CommandButton cmdStartup
- Caption = "Sta&rtup..."
- Height = 375
- Left = 6240
- TabIndex = 7
- Top = 1920
- Width = 1095
- End
- Begin VB.CommandButton cmdContinue
- Caption = "&Continue"
- Height = 375
- Left = 6240
- TabIndex = 6
- Top = 1440
- Width = 1095
- End
- Begin VB.CommandButton cmdPause
- Caption = "&Pause"
- Height = 375
- Left = 6240
- TabIndex = 5
- Top = 960
- Width = 1095
- End
- Begin VB.CommandButton cmdStop
- Caption = "S&top"
- Height = 375
- Left = 6240
- TabIndex = 4
- Top = 480
- Width = 1095
- End
- Begin VB.CommandButton cmdStart
- Caption = "&Start"
- Height = 375
- Left = 6240
- TabIndex = 3
- Top = 0
- Width = 1095
- End
- Begin MSComctlLib.ListView ListView
- Height = 2295
- Left = 120
- TabIndex = 0
- Top = 0
- Width = 6015
- _ExtentX = 10610
- _ExtentY = 4048
- View = 3
- LabelEdit = 1
- Sorted = -1 'True
- LabelWrap = -1 'True
- HideSelection = 0 'False
- FullRowSelect = -1 'True
- _Version = 393217
- ForeColor = -2147483640
- BackColor = -2147483643
- BorderStyle = 1
- Appearance = 1
- NumItems = 3
- BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628}
- Key = "Service"
- Text = "Service"
- Object.Width = 6174
- EndProperty
- BeginProperty ColumnHeader(2) {BDD1F052-858B-11D1-B16A-00C0F0283628}
- SubItemIndex = 1
- Key = "Status"
- Text = "Status"
- Object.Width = 2117
- EndProperty
- BeginProperty ColumnHeader(3) {BDD1F052-858B-11D1-B16A-00C0F0283628}
- SubItemIndex = 2
- Key = "Startup"
- Text = "Startup"
- Object.Width = 2117
- EndProperty
- End
- Begin VB.Label Label1
- Caption = "St&artup Parameters:"
- Height = 255
- Left = 120
- TabIndex = 1
- Top = 2400
- Width = 2895
- End
- Begin VB.Menu mnuFile
- Caption = "&File"
- Begin VB.Menu mnuFileSelectComputer
- Caption = "&Select Computer..."
- End
- Begin VB.Menu mnuFileSep1
- Caption = "-"
- End
- Begin VB.Menu mnuFileExit
- Caption = "E&xit"
- End
- End
- Begin VB.Menu mnuService
- Caption = "&Service"
- Begin VB.Menu mnuServiceStart
- Caption = "&Start"
- End
- Begin VB.Menu mnuServiceStop
- Caption = "S&top"
- End
- Begin VB.Menu mnuServicePause
- Caption = "&Pause"
- End
- Begin VB.Menu mnuServiceContinue
- Caption = "&Continue"
- End
- Begin VB.Menu mnuServiceStartup
- Caption = "Sta&rtup..."
- End
- Begin VB.Menu mnuServiceSep1
- Caption = "-"
- End
- Begin VB.Menu mnuServiceCreate
- Caption = "&Create..."
- End
- Begin VB.Menu mnuServiceDelete
- Caption = "&Delete"
- End
- Begin VB.Menu mnuServiceConfigure
- Caption = "Con&figure..."
- End
- End
- Attribute VB_Name = "frmMain"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- '****************************************************************************************************
- ' Copyright (c) Key Technology Pty Ltd 1999, All Rights Reserved.
- ' Web site: http://www.keytech.com.au Email: info@keytech.com.au
- '****************************************************************************************************
- Option Explicit
- ' The name of the computer whose services are being managed -
- ' an empty string means the local computer
- Private mComputerName As String
- Private Sub FillListView()
- On Error GoTo ErrorHandler
- ' Clear the list view of service information
- ListView.ListItems.Clear
- ' Open the service manager
- Dim ServiceManager As New ServiceManager
- ServiceManager.ComputerName = mComputerName
- ' Check if any services!
- Dim v As Variant
- v = ServiceManager.Services
- If VarType(v) <> (vbString Or vbArray) Then Exit Sub
- ' Display the service names and status
- Dim ServiceName() As String
- Dim i As Integer
- Dim ListItem As ListItem
- Dim Text As String
- ServiceName = v
- For i = LBound(ServiceName) To UBound(ServiceName)
- ' Service display name
- Dim DisplayName As String
-
- DisplayName = ServiceManager.DisplayName(ServiceName(i))
-
- Set ListItem = ListView.ListItems.Add(, , DisplayName)
-
- ' Remember the service name
- ListItem.Tag = ServiceName(i)
-
- ' Status
- Select Case ServiceManager.Status(ServiceName(i))
- Case Running
- Text = "Started"
-
- Case StopPending
- Text = "Stop Pending"
-
- Case Stopped
- Text = ""
-
- Case StartPending
- Text = "Start Pending"
-
- Case Paused
- Text = "Paused"
-
- Case PausePending
- Text = "Pause Pending"
-
- Case ContinuePending
- Text = "continue Pending"
-
- Case Else
- Text = "?"
- End Select
-
- ListItem.ListSubItems.Add , "Status", Text
-
- ' Startup
- Select Case ServiceManager.Configuration(ServiceName(i)).StartType
- Case AutoStart
- Text = "Automatic"
-
- Case BootStart
- Text = "Boot"
-
- Case DemandStart
- Text = "Manual"
-
- Case Disabled
- Text = "Disabled"
-
- Case SystemStart
- Text = "System Start"
-
- Case Else
- Text = "?"
- End Select
-
- ListItem.ListSubItems.Add , "Startup", Text
- Next
- If ListView.ListItems.Count <> 0 Then
- ListView.ListItems(1).Selected = True
- End If
- Exit Sub
- ErrorHandler:
- MsgBox Err.Description, vbExclamation
- End Sub
- Private Sub RefreshListItem(ServiceManager As ServiceManager, _
- ListItem As ListItem)
- On Error GoTo ErrorHandler
- Dim ServiceName As String
- Dim Text As String
- ServiceName = ListItem.Tag
- Select Case ServiceManager.Status(ServiceName)
- Case Running
- Text = "Started"
-
- Case StopPending
- Text = "Stop Pending"
-
- Case Stopped
- Text = ""
-
- Case StartPending
- Text = "Start Pending"
-
- Case Paused
- Text = "Paused"
-
- Case PausePending
- Text = "Pause Pending"
-
- Case ContinuePending
- Text = "continue Pending"
-
- Case Else
- Text = "?"
- End Select
- If Text <> ListItem.ListSubItems("Status") Then
- ListItem.ListSubItems("Status") = Text
- End If
- Exit Sub
- ErrorHandler:
- MsgBox Err.Description, vbExclamation
- End Sub
- Private Sub RefreshListView()
- On Error GoTo ErrorHandler
- ' Open the service manager
- Dim ServiceManager As New ServiceManager
- ServiceManager.ComputerName = mComputerName
- ' Update the status for each listed service
- Dim ListItem As ListItem
- Dim ServiceName As String
- Dim Text As String
- For Each ListItem In ListView.ListItems
- RefreshListItem ServiceManager, ListItem
- Next
- Exit Sub
- ErrorHandler:
- MsgBox Err.Description, vbExclamation
- End Sub
- Private Sub SetButtonState()
- On Error GoTo ErrorHandler
- ' Check if service selected
- If ListView.SelectedItem Is Nothing Then
- cmdStart.Enabled = False
- cmdStop.Enabled = False
- cmdPause.Enabled = False
- cmdContinue.Enabled = False
- cmdStartup.Enabled = False
-
- Exit Sub
- End If
- ' Get the service name
- Dim ServiceName As String
- ServiceName = ListView.SelectedItem.Tag
- ' Get the service extended status
- Dim ServiceManager As New ServiceManager
- Dim ExtendedServiceStatus As ExtendedServiceStatus
- ServiceManager.ComputerName = mComputerName
- Set ExtendedServiceStatus = ServiceManager.ExtendedStatus(ServiceName)
- ' Set the menu state
- cmdStart.Enabled = (ExtendedServiceStatus.Status = Stopped)
- cmdStop.Enabled = (ExtendedServiceStatus.ControlsAccepted And AcceptStop)
- cmdPause.Enabled = (ExtendedServiceStatus.ControlsAccepted And AcceptPauseContinue)
- cmdContinue.Enabled = (ExtendedServiceStatus.ControlsAccepted And AcceptPauseContinue)
- Exit Sub
- ErrorHandler:
- MsgBox Err.Description, vbExclamation
- End Sub
- Private Sub Form_Load()
- Timer.Enabled = False
- FillListView
- SetButtonState
- Timer.Enabled = True
- End Sub
- Private Sub cmdStart_Click()
- On Error GoTo ErrorHandler
- ' Get the service name
- If ListView.SelectedItem Is Nothing Then Exit Sub
- Dim ServiceName As String
- ServiceName = ListView.SelectedItem.Tag
- ' Get the optional startup arguments as a string array
- Dim ArgumentString() As String
- Dim Arguments As Variant
- If Len(txtParameters) <> 0 Then
- ArgumentString = Split(txtParameters, " ")
- Arguments = ArgumentString
- End If
- ' Start the service
- Dim ServiceManager As New ServiceManager
- ServiceManager.ComputerName = mComputerName
- ServiceManager.AccessMode = WriteAccess
- ServiceManager.Start ServiceName, Arguments
- RefreshListItem ServiceManager, ListView.SelectedItem
- Exit Sub
- ErrorHandler:
- MsgBox Err.Description, vbExclamation
- End Sub
- Private Sub cmdStop_Click()
- On Error GoTo ErrorHandler
- ' Get the service name
- If ListView.SelectedItem Is Nothing Then Exit Sub
- Dim ServiceName As String
- ServiceName = ListView.SelectedItem.Tag
- ' Stop the service
- Dim ServiceManager As New ServiceManager
- ServiceManager.ComputerName = mComputerName
- ServiceManager.AccessMode = WriteAccess
- ServiceManager.Stop ServiceName
- RefreshListItem ServiceManager, ListView.SelectedItem
- Exit Sub
- ErrorHandler:
- MsgBox Err.Description, vbExclamation
- End Sub
- Private Sub cmdPause_Click()
- On Error GoTo ErrorHandler
- ' Get the service name
- If ListView.SelectedItem Is Nothing Then Exit Sub
- Dim ServiceName As String
- ServiceName = ListView.SelectedItem.Tag
- ' Pause the service
- Dim ServiceManager As New ServiceManager
- ServiceManager.ComputerName = mComputerName
- ServiceManager.AccessMode = WriteAccess
- ServiceManager.Pause ServiceName
- RefreshListItem ServiceManager, ListView.SelectedItem
- Exit Sub
- ErrorHandler:
- MsgBox Err.Description, vbExclamation
- End Sub
- Private Sub cmdContinue_Click()
- On Error GoTo ErrorHandler
- ' Get the service name
- If ListView.SelectedItem Is Nothing Then Exit Sub
- Dim ServiceName As String
- ServiceName = ListView.SelectedItem.Tag
- ' Continue the service
- Dim ServiceManager As New ServiceManager
- ServiceManager.ComputerName = mComputerName
- ServiceManager.AccessMode = WriteAccess
- ServiceManager.Continue ServiceName
- RefreshListItem ServiceManager, ListView.SelectedItem
- Exit Sub
- ErrorHandler:
- MsgBox Err.Description, vbExclamation
- End Sub
- Private Sub cmdStartup_Click()
- On Error GoTo ErrorHandler
- If ListView.SelectedItem Is Nothing Then Exit Sub
- ' Prompt for startup information
- Dim frm As New frmStartup
- frm.Show vbModal, Me
- If Not frm.ConfigurationChanged Then Exit Sub
- ' Refresh the listview
- Dim ServiceManager As New ServiceManager
- ServiceManager.ComputerName = mComputerName
- RefreshListItem ServiceManager, ListView.SelectedItem
- Exit Sub
- ErrorHandler:
- MsgBox Err.Description, vbExclamation
- End Sub
- Private Sub ListView_Click()
- SetButtonState
- End Sub
- Private Sub mnuFileExit_Click()
- Unload Me
- End Sub
- Private Sub mnuFileSelectComputer_Click()
- ' Prompt for the computer name
- Dim frm As New frmSelectComputer
- frm.ComputerName = mComputerName
- frm.Show vbModal, Me
- If frm.ComputerName = mComputerName Then Exit Sub
- mComputerName = frm.ComputerName
- ' Refill the listview
- Timer.Enabled = False
- FillListView
- SetButtonState
- Timer.Enabled = True
- ' Set the caption
- Caption = "Service Manager Example #1"
- If Len(mComputerName) Then Caption = Caption & " - " & mComputerName
- End Sub
- Private Sub mnuService_Click()
- On Error GoTo ErrorHandler
- ' Check if service selected
- If ListView.SelectedItem Is Nothing Then
- mnuServiceStart.Enabled = False
- mnuServiceStop.Enabled = False
- mnuServicePause.Enabled = False
- mnuServiceContinue.Enabled = False
- mnuServiceStartup.Enabled = False
- mnuServiceDelete.Enabled = False
- mnuServiceConfigure.Enabled = False
-
- Exit Sub
- End If
- ' Get the service name
- Dim ServiceName As String
- ServiceName = ListView.SelectedItem.Tag
- ' Get the service extended status
- Dim ServiceManager As New ServiceManager
- Dim ExtendedServiceStatus As ExtendedServiceStatus
- ServiceManager.ComputerName = mComputerName
- Set ExtendedServiceStatus = ServiceManager.ExtendedStatus(ServiceName)
- ' Set the menu state
- mnuServiceStart.Enabled = (ExtendedServiceStatus.Status = Stopped)
- mnuServiceStop.Enabled = (ExtendedServiceStatus.ControlsAccepted And AcceptStop)
- mnuServicePause.Enabled = (ExtendedServiceStatus.ControlsAccepted And AcceptPauseContinue)
- mnuServiceContinue.Enabled = (ExtendedServiceStatus.ControlsAccepted And AcceptPauseContinue)
- mnuServiceDelete.Enabled = (ExtendedServiceStatus.Status = Stopped)
- mnuServiceConfigure.Enabled = (ExtendedServiceStatus.Status = Stopped)
- Exit Sub
- ErrorHandler:
- MsgBox Err.Description, vbExclamation
- End Sub
- Private Sub mnuServiceConfigure_Click()
- ' Get the service name
- If ListView.SelectedItem Is Nothing Then Exit Sub
- Dim ServiceName As String
- ServiceName = ListView.SelectedItem.Tag
- ' Prompt to configure a service
- Dim frm As New frmConfigure
- frm.ServiceName = ServiceName
- frm.Show vbModal, Me
- If Not frm.ConfigurationChanged Then Exit Sub
- ' Refill the listview
- Timer.Enabled = False
- FillListView
- SetButtonState
- Timer.Enabled = True
- End Sub
- Private Sub mnuServiceContinue_Click()
- cmdContinue_Click
- End Sub
- Private Sub mnuServiceCreate_Click()
- ' Prompt to create a service
- Dim frm As New frmConfigure
- frm.Show vbModal, Me
- If Not frm.ConfigurationChanged Then Exit Sub
- ' Refill the listview
- Timer.Enabled = False
- FillListView
- SetButtonState
- Timer.Enabled = True
- End Sub
- Private Sub mnuServiceDelete_Click()
- On Error GoTo ErrorHandler
- ' Get the service name
- If ListView.SelectedItem Is Nothing Then Exit Sub
- Dim ServiceName As String
- ServiceName = ListView.SelectedItem.Tag
- ' Prompt the user
- If MsgBox("Are you sure you want to delete the service " & ServiceName & "?", _
- vbExclamation Or vbYesNoCancel) <> vbYes Then Exit Sub
-
- ' Delete the service
- Dim ServiceManager As New ServiceManager
- ServiceManager.ComputerName = mComputerName
- ServiceManager.AccessMode = WriteAccess
- ServiceManager.Delete ServiceName
- ' Refill the listview
- Timer.Enabled = False
- FillListView
- SetButtonState
- Timer.Enabled = True
- Exit Sub
- ErrorHandler:
- MsgBox Err.Description, vbExclamation
- End Sub
- Private Sub mnuServicePause_Click()
- cmdPause_Click
- End Sub
- Private Sub mnuServiceStart_Click()
- cmdStart_Click
- End Sub
- Private Sub mnuServiceStartup_Click()
- cmdStartup_Click
- End Sub
- Private Sub mnuServiceStop_Click()
- cmdStop_Click
- End Sub
- Private Sub Timer_Timer()
- RefreshListView
- SetButtonState
- End Sub
- Public Property Get ComputerName() As String
- ComputerName = mComputerName
- End Property
- Public Property Get SelectedServiceName() As String
- If ListView.SelectedItem Is Nothing Then Exit Property
- SelectedServiceName = ListView.SelectedItem.Tag
- End Property
-